package com.binvshe.binvshe.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.common.http.BaseParserFragment;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.common.image.IML;
import com.binvshe.binvshe.entity.NoticeEntity;
import com.binvshe.binvshe.entity.NoticeEntityNum;
import com.f2prateek.dart.Dart;
import com.f2prateek.dart.InjectExtra;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.DateTimeUtil;
import com.pachong.android.frameworkbase.utils.SLog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.R.attr.data;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2017/2/6
 */

public class ActivityDetailsFragment extends BaseParserFragment<NoticeEntity> {

    private static final String KEY_ACTIVITYID = "key_activity_id";
    private TextView tvActDetails;
    private TextView tvActTimes;
    private TextView tvActAdresss;
    private  TextView tvActMoneys;
    @InjectExtra(KEY_ACTIVITYID)
    int mActivityID;
    private TextView tvtotalCount;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        View headerView = inflater.inflate(R.layout.frag_act_details_header, container, false);
        EventBus.getDefault().register(this);
        Dart.inject(this, getArguments());
        this.tvActDetails = (TextView) headerView.findViewById(R.id.tvActDetails);
        this.tvActTimes = (TextView) headerView.findViewById(R.id.tvActTimes);
        this.tvActAdresss = (TextView) headerView.findViewById(R.id.tvActAdresss);
        this.tvActMoneys = (TextView) headerView.findViewById(R.id.tvActMoneys);
        this.tvtotalCount =  (TextView) headerView.findViewById(R.id.tvtotalCount);
        getAdapter().setHeaderView(headerView);
//        initData();
        return rootView;
    }


    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public int getLastID(NoticeEntity data) {
        return data.getId();
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new NoticeAdapter(getContext());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getActivityData(ActivityEvent.DetailsEvent event) {
        tvActDetails.setText(event.getActivityEntity().getProvince()+"省 "+event.getActivityEntity().getCity()+"市 "+event.getActivityEntity().getAddress());
        tvActTimes.setText(event.getActivityEntity().getDuration());
        tvActAdresss.setText(event.getActivityEntity().getAddress());
        tvActMoneys.setText(event.getActivityEntity().getPrice());
        tvtotalCount.setText("当前共"+event.getActivityEntity().getNotice_count()+"条公告");
    }

    @Override
    public void onStartLoading() {
        getNoticeData();
    }

    private void getNoticeData() {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = UrlCenter.ACTIVITY_NOTE;
        UrlParams urlParams = new UrlParams();
        urlParams.put("size", PAGE_SIZE + "");
        urlParams.put("last_id", mlastid + "");
        urlParams.put("activity_id", mActivityID + "");
        request.get(url, urlParams, mRequestListener);
    }

    class NoticeAdapter extends RecyclerViewBaseAdapter<NoticeEntity> {


        public NoticeAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            return new NoticeHolder(setAdapterViewItem(parent, R.layout.item_activity_notice));
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
            NoticeHolder vh = (NoticeHolder) holder;
            vh.setDataView(this.getData().get(position));
        }

        class NoticeHolder extends RecyclerView.ViewHolder {
            @Bind(R.id.tvNoticeTitle)
            TextView mTvNoticeTitle;
            @Bind(R.id.tvNoticeContent)
            TextView mTvNoticeContent;
            @Bind(R.id.tvNoticeTime)
            TextView mTvNoticeTime;
            @Bind(R.id.ivCover)
            ImageView mIvCover;


            public NoticeHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }

            public void setDataView(NoticeEntity data) {
                mTvNoticeTitle.setText(data.getTitle());
                mTvNoticeContent.setText(data.getContent());
                mTvNoticeTime.setText(data.getCreateTime());
                IML.load(getContext(), mIvCover, data.getIcon());
            }
        }
    }

}
